import java.util.concurrent.*;

/**
 * @author sjw
 * @className Utils
 * @date 2021/2/24 10:06
 * @description: TODO
 */
public class Utils {
    public static String get(String url) throws InterruptedException {
        System.out.println("----get begin----");
        Thread.sleep(6000);
        System.out.println("----get end----");
        return Thread.currentThread().getId()+" result: "+url;
    }

    public static void blockingIO(){
        try {
            String result=get("blockingIO");
            System.out.println(result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }



    public static void futureBlockingIO(){
        FutureTask<String> futureTask=new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return get("futureBlockingIO");
            }
        });
        try {
            new Thread(futureTask).start();
            String result= futureTask.get();
            System.out.println(result);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }

    private static CompletableFuture<String> func1(){
        CompletableFuture<String> completableFuture=new CompletableFuture<>();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("begin");
                try {
                    Thread.sleep(6000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("end------");
                completableFuture.complete("func1");
            }
        }).start();
        return completableFuture;
    }

    public static void main(String[] args) {
        //blockingIO();
//        futureBlockingIO();
        try {
            CompletableFuture completableFuture=func1();
            System.out.println("123");
            System.out.println(completableFuture.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getId()+ "main end");
    }

}
